layui.config({
    base: baseUrl+'/js/'
}).extend({
    selectPlus: 'public/selectPlus/selectPlus',
    selectM: 'public/selectN/selectM',
    common: 'common' //如果 mymod.js 是在根目录，也可以不用设定别名
});

layui.use(['form','selectPlus','laydate','selectM','common'], function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer;
    $ = layui.jquery,
        selectPlus = layui.selectPlus,
        selectM = layui.selectM,
        laydate = layui.laydate,
        common = layui.common;


    $("#back").click(function () {
        var index=layer.getFrameIndex(window.name); //获取当前窗口的name
        layer.close(index);
    });


    var initRoles=[],initCallLineType=[];
    if(selCallLineTypeVal!=null && selCallLineTypeVal!=undefined){
        initCallLineType.push(selCallLineTypeVal);
        console.log(initCallLineType);
    }


    var callLineTypes,roles;
    //加载所有角色列表，供选择
    $.ajax({
        type: "POST",
        url:  baseUrl+'/role/getAllRole',
        dataType:"json",
        data:{},
        success: function (data) {
            //多选标签-所有配置
            roles = selectM({
                elem: '#roles'    //元素容器【必填】
                ,data: data     //候选数据【必填】
                ,selected: initRoles  //默认值
                ,max : 1  //最多选中个数，默认5
                ,name: 'roleId'     //input的name 不设置与选择器相同(去#.)
                ,delimiter: ','        //值的分隔符
                ,field: {idName:'id',titleName:'roleName'}            //候选项数据的键名
            });
        },
        error: function (msg) {
        }
    });

    /*
     加载列表，供选择
     */
    $.ajax({
        type: "POST",
        url:  baseUrl+'/userHasCallLine/getAllCallLineType',
        dataType:"json",
        data:{},
        success: function (data) {
            //多选标签-所有配置
            callLineTypes = selectM({
                elem: '#callLineTypes'    //元素容器【必填】
                ,data: data    //候选数据【必填】
                ,selected: initCallLineType   //默认值
                ,max : 1  //最多选中个数，默认5
                ,name: 'callLineType'     //input的name 不设置与选择器相同(去#.)
                ,delimiter: ','        //值的分隔符
                ,field: {idName:'value',titleName:'desc'}            //候选项数据的键名
            });
        },
        error: function (msg) {
        }
    });




    /**
     *
     *监听提交
     * */
    form.on('submit(save)', function(data){
        var load = layer.load();
        var data = data.field;

        var role = roles.selected;
        var callLineType = callLineTypes.selected;

        if(data['type']==1){
            if(role.length==0){
                layer.msg("请选择角色",{icon: 5});
                return false ;
            }
            $.each(role, function(i) {
                data['roleId']=role[i].id;
            });
        }else{
            console.log(callLineType.length);

            if(callLineType.length==0){
                layer.msg("请选择外呼线路类型",{icon: 5});
                return false ;
            }
            $.each(callLineType, function(i) {
                data['callLineType']=callLineType[i].value;
            });
        }
        console.log("提交数据:"+ JSON.stringify(data));
        $.ajax({
            type: "POST",
            url:  baseUrl+'/userHasCallLine/save',
            dataType:"json",
            data:data,
            success: function (data) {
                layer.close(load);
                if(data.code=="1"){
                    layer.msg(data.msg,{icon: 6});
                    window.parent.hasSave=true;
                    var index=layer.getFrameIndex(window.name); //获取当前窗口的name
                    layer.close(index);
                }else{
                    layer.msg(data.msg,{icon: 5})
                }
            },
            error: function (msg) {
                layer.close(load);
            }
        });
        return false;
    });


    //跟进人员员查询
    common.selectKeyup('selUserId','userName',baseUrl+'/users/selectUserList',document,form,function (data) {
        var html = '';
        for (var i = 0; i < data.length; i++) {
            var dataMap = data[i];
            html += '<option value="' +dataMap.id+'">'+dataMap.username+'</option>';
        }
        return html;
    },'<option value="" selected="">请通过姓名搜索</option>',null);

    form.on('select(type)', function(data){
        if(data.value==0){
            $("#selUserDiv").css("display","");
            $("#selRoleDiv").css("display","none");
        }else{
            $("#selUserDiv").css("display","none");
            $("#selRoleDiv").css("display","");
        }
    });
});
